package com.lotter.api.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lotter.common.exception.LotterCusException;
import com.lotter.common.invocation.BaseConditionVO;
import com.lotter.dao.ClickRecordMapper;
import com.lotter.dao.HourseNewsMapper;
import com.lotter.model.comment.ClickRecord;
import com.lotter.model.hoursenew.HouserNews;


@Service
public class HousernewsService {
	
	
	@Autowired
	private HourseNewsMapper hourseNewsMapper;
	
	@Autowired
	private ClickRecordMapper clickRecordMapper;
	
	
	/**
	 * 保存房间新闻信息
	 * @param houserNews
	 * @throws LotterCusException
	 */
	@Transactional(rollbackFor=Exception.class)
	public void insert(Long uid,String lrycis,String pics) throws LotterCusException{
		HouserNews housernews = new HouserNews();
		housernews.setCreatedate(new Date());
		housernews.setUid(uid);
		housernews.setHwinmoney(0L);
		housernews.setStepnum(0L);
		housernews.setTopnum(0L);
		housernews.setLrycis(lrycis);
		housernews.setPics(pics);
		housernews.setUpdatedate(new Date());
		hourseNewsMapper.insert(housernews);
	}
	
	/**
	 * 查询房间信息
	 * @param result
	 * @param vo
	 * @return
	 * @throws LotterCusException
	 */
	public Map<String,Object> findHousernews( Map<String,Object> result,BaseConditionVO vo,Long uid) 
			throws LotterCusException{
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("uid", uid);
		vo.setCondition(condition);
		List<HouserNews> list = hourseNewsMapper.findHouserNewsByPage(vo);
		result.put("data", list);
		return result;
	}
	
	
	/**
	 * 查询房间最新消息
	 * @param result
	 * @return
	 * @throws LotterCusException
	 */
	public HouserNews findHousernewsDto(Long uid)
	     throws LotterCusException{
		return hourseNewsMapper.findHouserNewsByUid(uid);
	}
	
	
	/**
	 * 顶一下 或者踩一下
	 * @param result
	 * @param id
	 * @param type
	 * @return
	 * @throws LotterCusException
	 */
	@Transactional(rollbackFor=Exception.class)
	public void updateTopORFoot(Long hid ,Long uid,int type)
	    throws LotterCusException{
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("id", hid);
		result.put("type", type);
		hourseNewsMapper.updateHourserTopFoot(result);
		
		ClickRecord clickRecord = new ClickRecord();
		clickRecord.setHid(hid);
		clickRecord.setUid(uid);
		clickRecord.setCreatedate(new Date());
		clickRecordMapper.insert(clickRecord);
	}
	 
	/**
	 * 根据主键查询数据
	 * @param id
	 * @return
	 * @throws LotterCusException
	 */
	public HouserNews selectByPrimaryKey(Long id) throws LotterCusException{
		return hourseNewsMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 更新
	 * @param id
	 * @param lrycis
	 * @param pics
	 * @throws LotterCusException
	 */
	@Transactional(rollbackFor=Exception.class)
	public void updateHousernews(Long id,String lrycis,
			String pics) throws LotterCusException{
		HouserNews houserNews = new HouserNews();
		houserNews.setId(id);
		houserNews.setLrycis(lrycis);
		houserNews.setPics(pics);
		houserNews.setUpdatedate(new Date());
		hourseNewsMapper.updateByPrimaryKeySelective(houserNews);
	}
	
	//用户新闻列表
	public List<HouserNews> findNewsList(Long uid,Integer numPerPage,Integer pageNum) throws LotterCusException{
		BaseConditionVO vo = new BaseConditionVO();
		vo.setKeywords(uid);
		vo.setNumPerPage(numPerPage);
		vo.setPageNum(pageNum);
		return hourseNewsMapper.findHouserNewsByPage(vo);
	}

}
